package back.controller;

import back.pojo.R;
import back.pojo.User;
import back.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/admin/")
public class UserController {
    @Autowired
    private UserService userService;
    private String msg="请求成功";

    /**
     * 处理用户访问错误路径
     * @return
     */
    @RequestMapping("/**")
    public String error(){
        return "error/404";
    }

    /**
     * 跳转到首页
     * @return
     */
    @GetMapping("/index")
    public String adminIndex() {
        return "/admin/index";
    }

    /**
     * 进入到main页面
     * @return
     */
    @GetMapping("/main")
    public String adminMain() {
        return "/admin/main";
    }

    /**
     * 用户管理页面
     * @return
     */
    @GetMapping("/user/index")
    public String adminUserList() {
        return "admin/user_manage";
    }

    /**
     * 查询用户信息列表
     * @param user 1、如果user各字段为空 查询全部用户信息；2、如果userName不为空 查询指定用户信息
     * @return
     */
    @PostMapping("/user/search")
    @ResponseBody
    public R getUserList(User user){
        // 查询用户所有的数量
        int total = userService.getCountUser(user);
        int pageSize=10;
        if (user.getPageSize()!=null && user.getPageSize()!=0){
            pageSize=user.getPageSize();
        }
        int pages=total/pageSize+(total%pageSize==0?0:1);
        // 查询的当前页
        user.updateSkip();
        List<User> userList = userService.getUsers(user);

        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("total",total);
        map.put("pages",pages);
        map.put("rows",userList);

        R r = new R();
        r.setCode(200);
        r.setMsg(msg);
        r.setData(map);
        return r;
    }

    /**
     * 添加用户
     * @param user 接收添加的用户信息
     * @return
     */
    @PostMapping("/user/add")
    @ResponseBody
    public R addUserForm(User user) {
        System.out.println("user = " + user);
        R r = new R();
        // 查询全部用户
        List<User> users = userService.getUsers(new User());
        System.out.println("users = " + users);
        for (User getUser : users) {
            if (!getUser.getUserName().equals(user.getUserName())){
                userService.addUser(user);
                r.setCode(200);
                r.setMsg("用户添加成功");
                r.setData(new HashMap<String, Object>());
                break;
            }else {
                r.setCode(401);
                r.setMsg("用户已存在");
                r.setData(null);
                break;
            }
        }
        return r;
    }

    /**
     * 通过id查询指定的用户信息
     * @param id
     * @return
     */
    @GetMapping("/user/{id}")
    @ResponseBody
    public R getUserById(@PathVariable int id){
        R r = new R();
        HashMap map = new HashMap<String, Object>();

        User user=userService.getUserById(id);
        map.put("data",user);

        r.setCode(200);
        r.setMsg(msg);
        r.setData(map);

        return r;
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PostMapping("/user/update")
    @ResponseBody
    public R updateUser(User user){
        R r = new R();
        HashMap map = new HashMap<String, Object>();
        map.put("data",user);

        userService.updateUser(user);
        r.setCode(200);
        r.setMsg(msg);
        r.setData(map);

        return r;
    }

    /**
     * 通过指定id删除用户
     * @param id
     * @return
     */
    @PostMapping("/user/del/{id}")
    @ResponseBody
    public R deleteUserById(@PathVariable int id){
        User user = userService.getUserById(id);

        userService.deleteUserById(id);

        R r = new R();
        HashMap map = new HashMap<String, Object>();
        map.put("data",user);
        r.setCode(200);
        r.setMsg("请求成功");
        r.setData(map);

        return r;
    }

    /**
     * 通过多个id批量删除用户信息
     * @param ids
     * @return
     */
    @PostMapping("/user/delete")
    @ResponseBody
    public R deleteUser(@RequestBody List<Integer> ids){
        R r = new R();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("data",ids);

        userService.deleteUser(ids);

        r.setCode(200);
        r.setMsg("");
        r.setData(map);

        return r;
    }



}
